package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * Created by luoxinmin on 2020/9/10.
 */
@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/t5")
    public String test4(){
        return "show";
    }

    /*
    路径传参：/{参数名}对应控制器方法里的@PathVariable("参数名称")--》控制器方法的形参
     */
    @RequestMapping("/t4/{ids}/{name}")
    public String test3(@PathVariable("name")String username,@PathVariable("ids") int id){
        System.out.println(username+"\t"+id);
        return "show";
    }

    /*
    实体收参：请参地址中参数名称与实体属性名一致
     */
    @RequestMapping("/t3")
    public String test2(User user){
        System.out.println("user.getName()+\"\\t\"+user.getPass() = " + user.getName()+"\t"+user.getPass());
        return "show";
    }

    /*
    地址传参：将地址中参数名称与控制器中方法的形参名相同
     */
    @RequestMapping("/t2")
    public String test1(int id, String name, String pass, HttpServletRequest request, @DateTimeFormat( pattern="yyyy-MM-dd") Date birth){
        System.out.println(name);
        String names=request.getParameter("name");
        System.out.println("names = " + names);
        try {
            names=new String(names.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(id+"\t"+names+"\t"+pass+"\t"+birth);
        return "show";
    }

    @RequestMapping("/t1")
    public String test(){
        System.out.println("test....");
        return "show";// /WEB-INF/view/test.jsp
    }


}
